﻿using BCCommon;
using BCCommon.Enums.Driver;
using BCCommon.TMS.Driver;
using BCDto.TMS.Driver;
using BCDto.TMS.Driver.Horseman;
using BCService.TMS.Driver;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace BCTMSRESTful.Controllers.Management.Driver
{
    /// <summary>
    /// 司机信息控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class DriversController : ManagementBaseController
    {
        private readonly IDriverService driverService;

        public DriversController(IDriverService driverService)
        {
            this.driverService = driverService;
        }

        #region 登记司机信息
        /// <summary>
        ///登记司机信息。
        /// </summary>
        /// <param name="dto">司机信息对象</param>
        /// <response code="201">已注册成功的司机信息。</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加司机账号")]
        [ProducesResponseType(typeof(DriverDto), 200)]
        public IActionResult CreateDriver([FromBody] DriverRequestDto dto)
        {
            var ip = Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = HttpContext.Connection.RemoteIpAddress.ToString();
            }
            DriverDto result = this.driverService.Add(this.Admin, dto, ip);

            return this.Created(string.Format("/api/v1/sim/Driver/{0}", result.DriverId), result);
        }



        /// <summary>
        ///登记骑手信息。
        /// </summary>
        /// <param name="dto">骑手信息对象</param>
        /// <response code="201">已注册成功的骑手信息。</response> 
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("添加骑手账号")]
        [ProducesResponseType(typeof(DriverDto), 200)]
        public IActionResult CreateHorseman([FromBody] HorsemanRequestDto dto)
        {
            var ip = Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = HttpContext.Connection.RemoteIpAddress.ToString();
            }
            DriverDto result = this.driverService.AddHorseman(this.Admin, dto, ip);

            return this.Created(string.Format("/api/v1/sim/Driver/{0}", result.DriverId), result);
        }
        #endregion

        #region 获取司机列表
        /// <summary>
        /// 获取司机信息列表（分页）
        /// </summary>
        /// <param name="phone">手机</param>
        /// <param name="name">司机姓名</param>
        /// <param name="vehicleNo">车牌号</param>
        /// <param name="provinceCode">省编码</param>
        /// <param name="cityCode">市编码</param>
        /// <param name="countyCode">区县编码</param>
        /// <param name="driverStatus">审核状态</param>       
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="role">司机类型</param>       
        /// <param name="ascending">是否升序排序</param>
        /// <param name="sortName">排序名称</param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页条数</param>
        /// <response code="200">带有分页信息的司机信息列表。</response> 
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<DriverDto>), 200)]
        public IActionResult Get(
            [FromQuery(Name = "phone")] string phone,
            [FromQuery(Name = "name")] string name,
            [FromQuery(Name = "vehicle_no")] string vehicleNo,
            [FromQuery(Name = "province_code")] string provinceCode,
            [FromQuery(Name = "city_code")] string cityCode,
            [FromQuery(Name = "county_code")] string countyCode,
            [FromQuery(Name = "driver_status")] DriverStatus? driverStatus,
            [FromQuery(Name = "start")] DateTime? start,
            [FromQuery(Name = "end")] DateTime? end,
            [FromQuery(Name = "role")] DriverRoleType? role,
            [FromQuery(Name = "ascending")] bool? ascending,
            [FromQuery(Name = "sort_name")] string sortName,
            [FromQuery(Name = "index")] int index = 1,
            [FromQuery(Name = "size")] int size = 10)
        {
            if (this.Admin.IsSystemAccount)
            {
                return this.Ok(this.driverService.GetList((long?)null, phone, name, vehicleNo, provinceCode, cityCode, countyCode, driverStatus, role, start, end, ascending, sortName, index, size));
            }
            return this.Ok(this.driverService.GetList(this.Admin.AgentRecord == null ? this.Admin.CompanyId : this.Admin.AgentRecord.CompanyId, phone, name, vehicleNo, provinceCode, cityCode, countyCode, driverStatus, role, start, end, ascending, sortName, index, size));

        }

        #endregion

        #region 获取所有已审批的司机
        /// <summary>
        /// 获取所有已审批的司机列表
        /// </summary>
        /// <returns></returns>
        /// <response code="200">获取所有已审批的司机列表成功。</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<DriverDto>), 200)]
        public IActionResult ALLApproved()
        {
            if (this.Admin.IsSystemAccount)
            {

                return this.Ok(this.driverService.GetALLApprovedList((long?)null));
            }

            return this.Ok(this.driverService.GetALLApprovedList(this.Admin.AgentRecord != null ? this.Admin.AgentRecord.CompanyId : this.Admin.CompanyId));

        }
        #endregion

        #region 查询指定的司机
        /// <summary>
        /// 获取司机信息
        /// </summary>
        /// <param name="driverId">司机Id</param>
        /// <returns></returns>
        /// <response code="200">查询指定的司机信息成功。</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        [HttpGet("{driver_id}")]
        [ProducesResponseType(typeof(DriverDto), 200)]
        public IActionResult Get([FromRoute(Name = "driver_id")] long driverId)
        {
            return Ok(driverService.GetDriverById(driverId));
        }
        #endregion

        #region 实名认证审核
        /// <summary>
        /// 实名认证审核通过
        /// </summary>
        /// <param name="driverId">司机Id</param>
        /// <param name="dto">审核信息</param>
        /// <returns></returns>
        /// <response code="200">实名认证审核成功。</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        [HttpPost("[action]/{driver_id}")]
        [Log("实名认证审核")]
        [ProducesResponseType(typeof(DriverDto), 200)]
        public IActionResult RealNameVerifiesApprove([FromRoute(Name = "driver_id")] long driverId, [FromBody] ApproveRequestDto dto)
        {
            return Ok(driverService.RealNameVerifiesApprove(this.Admin, driverId, dto));
        }
        #endregion

        #region 人脸识别认证审核
        /// <summary>
        /// 人脸识别认证审核
        /// </summary>
        /// <param name="driverId">司机Id</param>
        /// <param name="dto">审核信息</param>
        /// <returns></returns>
        /// <response code="200">人脸识别认证审核成功。</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        [HttpPost("[action]/{driver_id}")]
        [Log("人脸识别认证审核")]
        [ProducesResponseType(typeof(DriverDto), 200)]
        public IActionResult FaceVerifiesApprove([FromRoute(Name = "driver_id")] long driverId, [FromBody] ApproveRequestDto dto)
        {
            if (this.Admin.CompanyId != 1)
            {
                throw new ArgumentException("没有审核权限，需要联系总公司");
            }
            return Ok(driverService.FaceVerifiesApprove(this.Admin, driverId, dto));
        }
        #endregion

        #region 车辆审核
        /// <summary>
        /// 车辆审核审核
        /// </summary>
        /// <param name="driverId">司机Id</param>
        /// <param name="dto">审核信息</param>
        /// <returns></returns>
        /// <response code="200">人脸识别认证审核成功。</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <response code="500">服务器内部错误。</response>
        [HttpPost("[action]/{driver_id}")]
        [Log("车辆审核")]
        [ProducesResponseType(typeof(DriverDto), 200)]
        public IActionResult VehicleApprove([FromRoute(Name = "driver_id")] long driverId, [FromBody] ApproveRequestDto dto)
        {
            if (this.Admin.AgentRecord != null)
            {
            }
            return StatusCode((int)HttpStatusCode.OK, driverService.VehicleApprove(this.Admin, driverId, dto));
        }
        #endregion



    }
}
